{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ff37589",
   "metadata": {},
   "source": [
    "# 二进制响应内容\n",
    "\n",
    "* bytes\n",
    "* content 可以识别任何格式的网页数据，但是不利于分析（流媒体：如图片，视频，音频）\n",
    "* json()数据交换的常用表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a058a1b3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:23:54.544146Z",
     "start_time": "2022-03-07T12:23:54.361232Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "033b7e83",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:28:41.853306Z",
     "start_time": "2022-03-07T12:28:41.120542Z"
    }
   },
   "outputs": [],
   "source": [
    "response = requests.get(\"https://api.github.com/events\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e0749903",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:41:37.977826Z",
     "start_time": "2022-03-07T12:41:37.967385Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19fa1874",
   "metadata": {},
   "source": [
    "# Json 响应内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fad3b91b",
   "metadata": {},
   "source": [
    "# payload"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4c523faf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:39:24.822406Z",
     "start_time": "2022-03-07T12:39:24.144638Z"
    }
   },
   "outputs": [],
   "source": [
    "payload = {\"name\":\"weiwei\",\"password\":1323456}\n",
    "r = requests.get(\"http://httpbin.org/get\",params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "459f895b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:39:26.932987Z",
     "start_time": "2022-03-07T12:39:26.923658Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'http://httpbin.org/get?name=weiwei&password=1323456'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "621a7e73",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:40:02.982231Z",
     "start_time": "2022-03-07T12:40:02.972713Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\\n  \"args\": {\\n    \"name\": \"weiwei\", \\n    \"password\": \"1323456\"\\n  }, \\n  \"headers\": {\\n    \"Accept\": \"*/*\", \\n    \"Accept-Encoding\": \"gzip, deflate\", \\n    \"Host\": \"httpbin.org\", \\n    \"User-Agent\": \"python-requests/2.27.1\", \\n    \"X-Amzn-Trace-Id\": \"Root=1-6225fcfd-5314f9b75c02ee107c96dd9d\"\\n  }, \\n  \"origin\": \"120.85.40.109\", \\n  \"url\": \"http://httpbin.org/get?name=weiwei&password=1323456\"\\n}\\n'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7a88e128",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T12:41:46.122624Z",
     "start_time": "2022-03-07T12:41:46.112529Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Response.json of <Response [200]>>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d675416d",
   "metadata": {},
   "source": [
    "# 实践face++"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "efe0e018",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T13:00:37.848774Z",
     "start_time": "2022-03-07T13:00:37.206540Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2. 准备API的接口身份\n",
    "\n",
    "API_Key = \"UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P\"\n",
    "API_Secret = \"uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv\"\n",
    "\n",
    "# 3. 准备base URL（face detect）\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4. 请求方法：POST\n",
    "\n",
    "# 5. 准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://cn.bing.com/images/search?view=detailV2&ccid=uCaO0co9&id=57862D8B5A2F1DFF8C902DAA842BA770BF09DED5&thid=OIP.uCaO0co9eY5gOHoLYe2S_gHaLH&mediaurl=https%3a%2f%2fimg.alicdn.com%2ftfs%2fTB1A7Kwzq61gK0jSZFlXXXDKFXa-764-1146.png&exph=1146&expw=764&q=%e4%ba%ba%e8%84%b8%e5%9b%be%e7%89%87&simid=608044945621537453&FORM=IRPRST&ck=875AB7893FD170F3FF1807514CC55CA5&selectedIndex=0&ajaxhist=0&ajaxserp=0\",\n",
    "    \"return_landmark\":1,\n",
    "    \"return_attributes\":'gender,age,smiling'\n",
    "}\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5cdb0437",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T13:00:44.623109Z",
     "start_time": "2022-03-07T13:00:44.612084Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2f77e96c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-07T13:01:39.872732Z",
     "start_time": "2022-03-07T13:01:39.866727Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1646658038,6a402f2f-f85f-4dc0-a974-5f5c63969d5c\",\"time_used\":283,\"error_message\":\"IMAGE_ERROR_UNSUPPORTED_FORMAT: image_url\"}\\n'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c1ebbd8",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
